# -*- coding: utf-8 -*-

# pip install consul
from consul import Consul, Check

# pip install consulate
from consulate import Consul


def py_consul():
    # address = "192.168.10.153"
    address = "localhost"
    port = 8500
    name = "consul"
    id = "100001"
    tags = ["consul", "python", ]

    consul = Consul("localhost", 8500)

    # 健康检查：协议，主机，端口，地址，
    # 间隔时间：5s，超时时间：30s，注销时间：30s
    http_check = Check().http("http://localhost:5000/", "5s", "30s", "30s")
    consul.agent.test_func_param_type.register(name=name, service_id=id, address=address, port=port, tags=tags, check=http_check)
    services = consul.agent.services()

    service = services.get(id)
    url = "http://{0}:{1}".format(service['Address'], service['Port'])
    print("url=", url)

    pass


def py_consulate():
    address = "localhost"
    port = 8500
    name = "consulate"
    id = "100002"
    tags = ["consulate", "python", ]

    consul = Consul("localhost", 8500)

    consul.agent.test_func_param_type.register(name=name, service_id=id, address=address, port=port, tags=tags,
                                               httpcheck="http://localhost:5000/", interval=5)  # 间隔时间单位秒
    services = consul.agent.services()[0]

    service = services.get(id)
    url = "http://{0}:{1}".format(service['Address'], service['Port'])
    print("url=", url)

    pass


if __name__ == '__main__':
    py_consul()
    # py_consulate()
    pass
